using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelGuide : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private Image page;
        private int currIndex;
        
        private static bool HasShow
        {
            get
            {
                return PlayerPrefs.GetInt("PanelGuide_Show") != 0;
            }
            set
            {
                PlayerPrefs.SetInt("PanelGuide_Show", value ? 1 : 0);
            }
        }

        public static void Open()
        {
            if (HasShow) return;
            UIManagerComponent.instance.OpenUI<PanelGuide>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelGuide>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelGuide", layer, isFloat);
            BindProperties(transform);

            Page_EN.gameObject.SetActive(false);
            Page_BR.gameObject.SetActive(false);
            switch (I18N.CurrLanguage)
            {
                case ELanguage.ZH_CN:
                    page = Page_EN;
                    break;
                case ELanguage.EN_US:
                    page = Page_EN;
                    break;
                case ELanguage.PT_BR:
                    page = Page_BR;
                    break;
                case ELanguage.VI_VN:
                    page = Page_EN;
                    break;
            }
            page.gameObject.SetActive(true);
            page.sprite = page.gameObject.Get<Sprite>(0);
            
            BtnPrev.gameObject.SetActive(false);
            BtnNext.gameObject.SetActive(true);
            BtnEnter.gameObject.SetActive(false);
            
            BtnPrev.SetClick(() =>
            {
                currIndex--;
                RefreshPage();
            });

            BtnNext.SetClick(() =>
            {
                currIndex++;
                RefreshPage();
            });
            
            BtnEnter.SetClick(() =>
            {
                HasShow = true;
                Close();
            });
        }

        private void RefreshPage()
        {
            BtnPrev.gameObject.SetActive(currIndex > 0);
            BtnNext.gameObject.SetActive(currIndex < 4);
            BtnEnter.gameObject.SetActive(currIndex == 4);
            page.sprite = page.gameObject.Get<Sprite>(currIndex);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
